package name.matthewgreet.example5.util; import java.util.Collection; import java.util.List; import javax.servlet.http.HttpSession; import org.apache.commons.lang3.StringUtils; import name.matthewgreet.example5.action.FindGameListByDesignerNameAction; import name.matthewgreet.example5.action.FindGameListByFamilyAction; import name.matthewgreet.example5.action.FindGameListByNameContainsAction; import name.matthewgreet.example5.action.FindGameListByRankAction; import name.matthewgreet.example5.action.FindUserRatingListByRatingAction; import name.matthewgreet.example5.action.FindUserRatingListByUsernameContainsAction; import name.matthewgreet.example5.action.ViewGameListAction; import name.matthewgreet.example5.action.ViewRatingDistributionListAction; import name.matthewgreet.example5.action.ViewUserRatingListAction; import name.matthewgreet.example5.action.FindGameListByRankAction.GameListSizeFinderByRank; import name.matthewgreet.example5.action.FindGameListByRankAction.GamePageFinderByRank; import name.matthewgreet.example5.action.FindUserRatingListByRatingAction.UserRatingByRatingListFinder; import name.matthewgreet.example5.action.ViewGameListAction.GameCategory; import name.matthewgreet.example5.common.AbstractCompositeCache_2S; import name.matthewgreet.example5.common.ListFinder; import name.matthewgreet.example5.common.NA; import name.matthewgreet.example5.common.PageByIdsFinder; import name.matthewgreet.example5.common.PageExtensionAssembler; import name.matthewgreet.example5.common.SingleItemFinder; import name.matthewgreet.example5.common.ListCache.ListCacheConfig; import name.matthewgreet.example5.common.ListCache.PaginationMode; import name.matthewgreet.example5.common.ListCacheRecordComparator; import name.matthewgreet.example5.dto.GameDTO; import name.matthewgreet.example5.dto.GameRatingDTO; import name.matthewgreet.example5.dto.UserRatingDTO; import name.matthewgreet.example5.model.DesignerTable; import name.matthewgreet.example5.model.GameRatingPK; import name.matthewgreet.example5.model.GameRatingTable; import name.matthewgreet.example5.model.GameTable; import name.matthewgreet.example5.model.Model; import name.matthewgreet.example5.model.UserRatingPK; import name.matthewgreet.example5.model.UserTable; import name.matthewgreet.example5.model.GameTable.GameOption; /** * * * <STYLE>TABLE {border-collapse: collapse;} TD,TH {border: 1px solid black; text-align: left;}</STYLE> * <TABLE> * <TR> * <TH>Slave list no</TH> * <TH>Name</TH> * <TH>Description</TH> * <TH>Viewer action</TH> * <TH>Base record</TH> * <TH>Other base record data</TH> * <TH>Page extension data</TH> * <TH>Detail data</TH> * <TH>Pagination mode</TH> * <TH>List/id/size finder</TH> * <TH>Page finder/ page extension assembler</TH> * <TH>Single item finder</TH> * </TR> * <TR> * <TD>Master</TD> * <TD>Board games</TD> * <TD>Board games in Board Game Geek database up to 2022</TD> * <TD><CODE>{@link ViewGameListAction}</CODE></TD> * <TD><CODE>{@link GameDTO}</CODE></TD> * <TD> </TD> * <TD>Related designers</TD> * <TD>Related artists, mechanics, and publishers</TD> * <TD ALIGN="CENTER">Depends on search criteria</TD> * <TD ALIGN="CENTER">Supplied by {@link FindGameListByDesignerNameAction}, {@link FindGameListByFamilyAction}, {@link FindGameListByNameContainsAction} and {@link FindGameListByRankAction}</TD> * <TD ALIGN="CENTER">{@link GamePageByIdsFinder}, {@link GamePageExtensionAssembler}</TD> * <TD ALIGN="CENTER">{@link GameItemFinder}</TD> * </TR> * <TR> * <TD>1</TD> * <TD>Ratings distribution</TD> * <TD>Counts of each user ratings of 0 to 10</TD> * <TD><CODE>{@link ViewRatingDistributionListAction}</CODE></TD> * <TD><CODE>{@link GameRatingDTO}</CODE></TD> * <TD> </TD> * <TD> </TD> * <TD> </TD> * <TD ALIGN="CENTER">Full list</TD> * <TD ALIGN="CENTER">{@link GameRatingByGameIdListFinder}</TD> * <TD ALIGN="CENTER">N/A</TD> * <TD ALIGN="CENTER"> </TD> * </TR> * <TR> * <TD>2</TD> * <TD>User ratings</TD> * <TD>Individual user ratings</TD> * <TD><CODE>{@link ViewUserRatingListAction}</CODE></TD> * <TD>{@link UserRatingDTO}</TD> * <TD> </TD> * <TD>Owning username</TD> * <TD> </TD> * <TD ALIGN="CENTER">Base record list</TD> * <TD ALIGN="CENTER">Supplied by {@link FindUserRatingListByUsernameContainsAction} and {@link FindUserRatingListByRatingAction}</TD> * <TD ALIGN="CENTER">{@link UserRatingPageExtensionAssembler}</TD> * <TD ALIGN="CENTER"> </TD> * </TR> * </TABLE> */ public class GameCompositeCache extends AbstractCompositeCache_2S<Integer,GameDTO,GameRatingPK,GameRatingDTO,UserRatingPK,UserRatingDTO> { public enum GameSortColumn { RANK, NAME, YEAR(true), WEIGHT, RATING, OWNERS, CATEGORIES, DESIGNERS; private boolean defaultDescending; private GameSortColumn() { this.defaultDescending = false; } private GameSortColumn(boolean defaultDescending) { this.defaultDescending = defaultDescending; } public boolean getDefaultDescending() { return defaultDescending; } } public static class GameItemFinder implements SingleItemFinder<Integer,GameDTO> { private static final long serialVersionUID = -8352500134074838922L; @Override public GameDTO getItem(Integer key) throws Exception { GameTable gameTable; GameDTO result; gameTable = Model.getInstance().getGameTable(); result = gameTable.find(key, GameOption.ARTISTS, GameOption.DESIGNERS, GameOption.MECHANICS, GameOption.PUBLISHERS); return result; } } public static abstract class GameItemSorter implements ListCacheRecordComparator<GameDTO> { private static final long serialVersionUID = -8303476010536609145L; private GameSortColumn sortColumn; private boolean ascending; protected GameItemSorter(GameSortColumn sortColumn, boolean ascending) { this.sortColumn = sortColumn; this.ascending = ascending; } public boolean usesPageExtensionData() { switch (sortColumn) { case DESIGNERS: return true; case CATEGORIES: case NAME: case OWNERS: case RANK: case RATING: case WEIGHT: case YEAR: return false; } return true; } public GameSortColumn getSortColumn() { return sortColumn; } public boolean getAscending() { return ascending; } } public static class GameItemSorterCategories extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; public GameItemSorterCategories(boolean ascending) { super(GameSortColumn.CATEGORIES, ascending); } @Override public int compare(GameDTO o1, GameDTO o2) { String earliestCategory1, earliestCategory2; int result; earliestCategory1 = FormatLibrary.earliestCategoryName(o1.getRankAbstract() != null, o1.getRankChildren() != null, o1.getRankCustomizable() != null, o1.getRankFamily() != null, o1.getRankParty() != null, o1.getRankStrategy() != null, o1.getRankThematic() != null, o1.getRankWar() != null); earliestCategory2 = FormatLibrary.earliestCategoryName(o2.getRankAbstract() != null, o2.getRankChildren() != null, o2.getRankCustomizable() != null, o2.getRankFamily() != null, o2.getRankParty() != null, o2.getRankStrategy() != null, o2.getRankThematic() != null, o2.getRankWar() != null); result = StringUtils.compareIgnoreCase(earliestCategory1, earliestCategory2); if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } } public static class GameItemSorterDesigners extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; public GameItemSorterDesigners(boolean ascending) { super(GameSortColumn.DESIGNERS, ascending); } @Override public int compare(GameDTO o1, GameDTO o2) { String earliestDesigner1, earliestDesigner2; int result; earliestDesigner1 = FormatLibrary.earliestDesigner(o1.getDesigners()); earliestDesigner2 = FormatLibrary.earliestDesigner(o2.getDesigners()); result = StringUtils.compareIgnoreCase(earliestDesigner1, earliestDesigner2); if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } } public static class GameItemSorterName extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; public GameItemSorterName(boolean ascending) { super(GameSortColumn.NAME, ascending); } @Override public int compare(GameDTO o1, GameDTO o2) { int result; result = StringUtils.compareIgnoreCase(o1.getName(), o2.getName()); if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } } public static class GameItemSorterOwners extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; public GameItemSorterOwners(boolean ascending) { super(GameSortColumn.OWNERS, ascending); } @Override public int compare(GameDTO o1, GameDTO o2) { int result; result = o1.getOwned() - o2.getOwned(); if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } } public static class GameItemSorterRank extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; private GameCategory preferredCategory; public GameItemSorterRank(GameCategory preferredCategory, boolean ascending) { super(GameSortColumn.RANK, ascending); this.preferredCategory = preferredCategory; } @Override public int compare(GameDTO o1, GameDTO o2) { Integer rank1, rank2; int result; rank1 = null; rank2 = null; switch (preferredCategory) { case ABSTRACT: rank1 = o1.getRankAbstract(); rank2 = o2.getRankAbstract(); break; case ALL: rank1 = o1.getRankAll(); rank2 = o2.getRankAll(); break; case CHILDREN: rank1 = o1.getRankChildren(); rank2 = o2.getRankChildren(); break; case CUSTOMIZABLE: rank1 = o1.getRankCustomizable(); rank2 = o2.getRankCustomizable(); break; case FAMILY: rank1 = o1.getRankFamily(); rank2 = o2.getRankFamily(); break; case PARTY: rank1 = o1.getRankParty(); rank2 = o2.getRankParty(); break; case STRATEGY: rank1 = o1.getRankStrategy(); rank2 = o2.getRankStrategy(); break; case THEMATIC: rank1 = o1.getRankThematic(); rank2 = o2.getRankThematic(); break; case WAR: rank1 = o1.getRankWar(); rank2 = o2.getRankWar(); break; } if (rank1 != null && rank2 != null) { result = rank1.intValue() - rank2.intValue(); } else if (rank1 != null) { result = -1; } else if (rank2 != null) { result = 1; } else { result = 0; } if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } public GameCategory getPreferredCategory() { return preferredCategory; } public void setPreferredCategory(GameCategory preferredCategory) { this.preferredCategory = preferredCategory; } } public static class GameItemSorterRating extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; public GameItemSorterRating(boolean ascending) { super(GameSortColumn.RATING, ascending); } @Override public int compare(GameDTO o1, GameDTO o2) { int result; result = o1.getAvgRating().compareTo(o2.getAvgRating()); if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } } public static class GameItemSorterWeight extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; public GameItemSorterWeight(boolean ascending) { super(GameSortColumn.WEIGHT, ascending); } @Override public int compare(GameDTO o1, GameDTO o2) { int result; result = o1.getGameWeight().compareTo(o2.getGameWeight()); if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } } public static class GameItemSorterYear extends GameItemSorter { private static final long serialVersionUID = 2419214141243354300L; public GameItemSorterYear(boolean ascending) { super(GameSortColumn.YEAR, ascending); } @Override public int compare(GameDTO o1, GameDTO o2) { int result; result = o1.getYearPublished() - o2.getYearPublished(); if (result == 0) { result = o1.getId() - o2.getId(); } return getAscending()?result:-result; } } public static class GamePageByIdsFinder implements PageByIdsFinder<Integer,GameDTO> { private static final long serialVersionUID = 4032331794734656685L; @Override public Collection<GameDTO> getItems(Collection<Integer> keys) throws Exception { GameTable gameTable; Collection<GameDTO> result; gameTable = Model.getInstance().getGameTable(); result = gameTable.findByIds(keys, GameOption.DESIGNERS); return result; } } public static class GamePageExtensionAssembler implements PageExtensionAssembler<GameDTO> { private static final long serialVersionUID = 2206130689287406339L; @Override public void assemblePageExtensions(Collection<GameDTO> items) throws Exception { DesignerTable designerTable; designerTable = Model.getInstance().getDesignerTable(); for (GameDTO game: items) { game.setDesigners(designerTable.findByGameId(game.getId())); } } } public static class GameRatingByGameIdListFinder implements ListFinder<GameDTO,GameRatingDTO> { private static final long serialVersionUID = 6409312894523453002L; @Override public List<GameRatingDTO> getList(GameDTO selectedMaster) throws Exception { GameRatingTable gameRatingTable; gameRatingTable = Model.getInstance().getGameRatingTable(); return gameRatingTable.findByGameId(selectedMaster.getId()); } } public static class UserRatingPageExtensionAssembler implements PageExtensionAssembler<UserRatingDTO> { private static final long serialVersionUID = -7275341565885053704L; @Override public void assemblePageExtensions(Collection<UserRatingDTO> items) throws Exception { UserTable userTable; userTable = Model.getInstance().getUserTable(); for (UserRatingDTO userRating: items) { userRating.setUser(userTable.findById(userRating.getUserId()).getDTO()); } } } private static final long serialVersionUID = -6454993304545014831L; private static final String ATTR_NAME_CACHE = GameCompositeCache.class + "_CACHE"; public synchronized static GameCompositeCache getInstance(HttpSession session) { GameCompositeCache instance; instance = (GameCompositeCache)session.getAttribute(ATTR_NAME_CACHE); if (instance == null) { instance = new GameCompositeCache(); session.setAttribute(ATTR_NAME_CACHE, instance); } return instance; } @Override protected ListCacheConfig<NA,Integer,GameDTO> getMasterListCacheConfig() { ListCacheConfig<NA,Integer,GameDTO> result; result = new ListCacheConfig<NA,Integer,GameDTO>(); result.setHasDetails(true); result.setItemFinder(new GameItemFinder()); result.setItemSorter(null); result.setKeyExtractor(g -> g.getId()); result.setListSizeFinder(new GameListSizeFinderByRank(GameCategory.ALL, null, null)); result.setName("Board games"); result.setPageByIdsFinder(new GamePageByIdsFinder()); result.setPageByIndexRangeFinder(new GamePageFinderByRank(GameCategory.ALL, null, null)); result.setPageExtensionAssembler(new GamePageExtensionAssembler()); result.setPageSize(ViewGameListAction.PAGE_SIZE); result.setPaginationMode(PaginationMode.PAGE_BY_INDEX_RANGE); return result; } @Override protected ListCacheConfig<GameDTO, GameRatingPK, GameRatingDTO> getSlaveListCache1Config() { ListCacheConfig<GameDTO, GameRatingPK, GameRatingDTO> result; result = new ListCacheConfig<GameDTO, GameRatingPK, GameRatingDTO>(); result.setHasDetails(false); result.setKeyExtractor(gr -> new GameRatingPK(gr.getGameId(),gr.getRating())); result.setListFinder(new GameRatingByGameIdListFinder()); result.setName("Ratings distribution"); result.setPaginationMode(PaginationMode.FULL_LIST); return result; } @Override protected ListCacheConfig<GameDTO, UserRatingPK, UserRatingDTO> getSlaveListCache2Config() { ListCacheConfig<GameDTO, UserRatingPK, UserRatingDTO> result; result = new ListCacheConfig<GameDTO, UserRatingPK, UserRatingDTO>(); result.setBaseRecordListFinder(new UserRatingByRatingListFinder(null)); result.setHasDetails(false); result.setKeyExtractor(ur -> new UserRatingPK(ur.getGameId(),ur.getUserId())); result.setName("Individual user ratings"); result.setPageExtensionAssembler(new UserRatingPageExtensionAssembler()); result.setPageSize(ViewUserRatingListAction.PAGE_SIZE); result.setPaginationMode(PaginationMode.BASE_RECORD_LIST); return result; } }